/**
 * @file TimerTask.hpp
 * @author 1102403684@qq.com
 * @brief 
 * @version 0.1
 * @date 2024-09-30
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#pragma once

#include <cstdint>
#include <functional>
#include <memory>
#include "../common/logger.hpp"

using TaskFunc = std::function<void()>;
using ReleaseFunc = std::function<void()>;

class TimerTask
{
public:
    using ptr = std::shared_ptr<TimerTask>;
    using ptr_weak = std::weak_ptr<TimerTask>;

    TimerTask(uint64_t id, uint32_t delay, const TaskFunc &task_func)
        : id_(id), time_out_(delay), task_func_(task_func), cancel_(false)
    {
    }

    ~TimerTask()
    {
        if(cancel_) return;

        LOG_TRACE("定时任务执行");
        task_func_();
        release_func_();
    }

    void setRelease(const ReleaseFunc& release_func)
    {
        release_func_ = release_func;
    }

    uint32_t DelayTime(){
        return time_out_;
    } 

    void Cancel(){
        cancel_ = true;
    }

private:
    uint64_t id_;              // 定时器任务对象ID
    uint32_t time_out_;        // 定时任务超时时间
    bool cancel_; //定时任务是否被取消
    TaskFunc task_func_;       // 定时任务
    ReleaseFunc release_func_; // 用于删除Timerwheel中保存的对象信息

};